﻿
#include <iostream>
#include <thread>
#include <stdlib.h>
#include "../include/sdkcommand.h"

#ifdef _MSC_VER
#ifndef FD_SETSIZE
#define FD_SETSIZE 1024
#endif
#include <Windows.h>

#else
#include <string.h>
#endif

std::string unicodetoutf8(std::wstring wsrc)
{
#ifdef _MSC_VER
    char* m_char;
    int len = WideCharToMultiByte(CP_UTF8, 0, wsrc.c_str(), (int)wsrc.length(), NULL, 0, NULL, NULL);
    m_char = new char[len + 1];            //checked
    WideCharToMultiByte(CP_UTF8, 0, wsrc.c_str(), (int)wsrc.length(), m_char, len, NULL, NULL);
    m_char[len] = '\0';
    std::string out = m_char;
    delete[]m_char;
    return out;
#else
    std::wstring wstr = wsrc;

    size_t len = wstr.size() * 4;
    setlocale(LC_CTYPE, "");
    char* buf = new char[len];             //checked

    wcstombs(buf, wstr.c_str(), len);
    std::string out(buf);
    delete[] buf;
    return out;
#endif
}

//#define _CLOUD  //接入云平台

void TestCloudCommand()
{
    //获取声卡列表
    //std::cout << sdkcommand("getsoundcard") <<std::endl;

    //获取语音列表
    //std::cout << sdkcommand("gettts") << std::endl;

    //播放本文件
    //std::cout << sdkcommand("play -id 1 -s test.wav -d 1 -v 30");

    //局域网点播服务器文件(filemap.xml)
    //std::cout << sdkcommand("net -id 2 -node 1 -s 10001 -d 1 -v 50");

    //播放MIC
    //std::cout << sdkcommand("live -id 3 -s 1 -d 1 -v 50");

    //播放虚拟MIC(推音频流)
    //std::cout << sdkcommand("live -id 13 -s 103 -d 1 -v 50");

    //停止广播
    //std::cout << sdkcommand("stop -id 1");

    //播放文字TTS
    std::wstring strWCmd = L"请穿黑色长袖黑色下装的男士佩戴口罩";
    std::string strCmd = u8"tts -net -id 1 -r 108 -l 1 -d 38 -v 10 -i 10 -s @$" + unicodetoutf8(strWCmd.c_str()) + u8"@$";
    std::cout << sdkcommand(strCmd.c_str());

    //录音(范围101到10000)，生成文件如101.wav
    //std::string strCmd = "net -id 1 -node 230 -s 1 -d 2000.101";

    //上传配置(状态返回 uploadconfig -node 3 -s 0)， 0：正确 1：配置错误 2:套接字错误 3:网络连接错误
    //std::cout << sdkcommand("uploadconfig");

    //局域网音箱固件升级(升级工作目录speaker.zip升级包)， 状态 0：正确 1：配置错误 2:套接字错误 3:网络连接错误
    //std::cout << sdkcommand("upgrade -ip 192.168.0.230");

    //局域网设置IP地址， 状态 0：正确 1：配置错误 2:套接字错误 3:网络连接错误
    //std::cout << sdkcommand("setip -ip 192.168.0.231 -inet 192.168.0.230 -netmask 255.255.255.0 -gateway 192.168.0.1");

    //局域网扫描，设置IP地址(固件2.7.10支持)
    //std::cout << sdkcommand("scansetip -node 020206bdf381 -domain beilie.media -ip 192.168.0.231 -netmask 255.255.255.0 -gateway 192.168.0.1");

    //局域网扫描，试听(固件2.7.10支持)
    //std::cout << sdkcommand("scanlisten -node 020206bdf381");

    //局域网扫描，重启(固件2.7.10支持)
    //std::cout << sdkcommand("scanreboot -node 020206bdf381");
}

void TestLocalCommand()
{
    //获取声卡列表
    //std::cout << sdkcommand("getsoundcard") <<std::endl;

    //获取语音列表
    //std::cout << sdkcommand("gettts") << std::endl;

    //播放本文件
    //std::cout << sdkcommand("play -id 1 -s test.wav -d 1 -v 50");

    //局域网点播服务器文件(filemap.xml)
    //std::cout << sdkcommand("net -id 2 -node 1 -s 10001 -d 1 -v 50");

    //播放MIC
    //std::cout << sdkcommand("live -id 3 -s 1 -d 1 -v 50");

    //播放虚拟MIC(推音频流)
    //std::cout << sdkcommand("live -id 13 -s 103 -d 1 -v 50");

    //停止广播
    //std::cout << sdkcommand("stop -id 1");

#ifdef WIN32
    /*
    //播放文字TTS
    for (int i = 0; i < 10; i++)
    {
        char c[20];
        sprintf_s(c, "%d", i+1);
        if (i % 2)
        {
            std::wstring strWCmd = L"请穿黑色长袖黑色下装的男士佩戴口罩";
            std::string strCmd = u8"tts -id " + std::string(c) + " -r 10 -l 1 -d 4 -v 20 -i 5 -s @$" + unicodetoutf8(strWCmd.c_str()) + u8"@$";
            std::cout << sdkcommand(strCmd.c_str());
        }
        else
        {
            std::wstring strWCmd = L"作为人类文化的重要组成部分";
            std::string strCmd = u8"tts -id " + std::string(c) + " -r 10 -l 1 -d 5 -v 20 -i 5 -s @$" + unicodetoutf8(strWCmd.c_str()) + u8"@$";
            std::cout << sdkcommand(strCmd.c_str());
        }
    }
    */
#endif
    //录音(范围101到10000)，生成文件如101.wav
    //std::string strCmd = "net -id 1 -node 230 -s 1 -d 2000.101";

    //上传配置(状态返回 uploadconfig -node 3 -s 0)， 0：正确 1：配置错误 2:套接字错误 3:网络连接错误
    //std::cout << sdkcommand("uploadconfig");

    //局域网音箱固件升级(升级工作目录speaker.zip升级包)， 状态 0：正确 1：配置错误 2:套接字错误 3:网络连接错误
    //std::cout << sdkcommand("upgrade -ip 192.168.0.230");

    //局域网设置IP地址， 状态 0：正确 1：配置错误 2:套接字错误 3:网络连接错误
    //std::cout << sdkcommand("setip -ip 192.168.0.231 -inet 192.168.0.230 -netmask 255.255.255.0 -gateway 192.168.0.1");

    //局域网扫描，设置IP地址(固件2.7.10支持)
    //std::cout << sdkcommand("scansetip -node 020206bdf381 -domain beilie.media -ip 192.168.0.231 -netmask 255.255.255.0 -gateway 192.168.0.1");

    //局域网扫描，试听(固件2.7.10支持)
    //std::cout << sdkcommand("scanlisten -node 020206bdf381");

    //局域网扫描，重启(固件2.7.10支持)
    //std::cout << sdkcommand("scanreboot -node 020206bdf381");
}

void notify()
{
	while (true)
	{
        std::string strNotify = sdknotify();
        std::cout << strNotify.c_str() << std::endl;
        if (strNotify == "sysstate -node 000000000000 -s online")
        {
#ifdef _CLOUD
            TestCloudCommand();  //云端命令需要登录成功后才执行
#endif
        }
	}
}

int main()
{
    std::thread t1(notify);

#ifdef _CLOUD
    const char *ret = sdkcommand("init -domain 192.168.0.110 -path C:\\Users\\Administrator\\Documents\\ZIBON\\AudioClient\\config\\ -debug");
#else
#ifdef WIN32
    const char *ret = sdkcommand("init -node 300 -ip 192.168.0.196 -path D:/ -debug");
#else
    const char *ret = sdkcommand("init -node 300 -ip ens33 -path /mnt/hgfs/D/ -debug -noalsa");
#endif
#endif
    std::string strRet = ret;
    std::cout << strRet.c_str() << std::endl;
    if (strRet != "OK")
    {
        std::cout << "SDK init error." << std::endl;
        return 1;
    }
    
#ifdef _CLOUD
    sdkcommand("login -user Administrator -pass 123");  //云服务器登录
#else
    TestLocalCommand();
#endif

	std::cout << "#";
	char cmd[1024];
	while (std::cin.getline(cmd, 1024))
	{
        if (strlen(cmd) == 0)
        {
            std::cout << "#";
            continue;
        }
		std::cout << sdkcommand(cmd) << std::endl;
		std::cout << "#";
	}
	return 0;
}